export class MemoryStream {
  public constructor() {
    this.arr = [];
    this.length = 0;
    this.Position = 0;
  }
  public arr: number[];
  public length: number;
  public Position: number;
  public Write(buffer: number[], offset: number, count: number) {
    for (let i = 0; i < count; i++) this.arr[this.Position++] = buffer[offset + i];
    if (this.Position > this.length) this.length = this.Position;
  }
  public Read(buffer: number[], offset: number, count: number) {
    for (let i = 0; i < count; i++) buffer[offset + i] = this.arr[this.Position++];
  }
  public SetLength(value: number) {
    if (this.length == value) return;
    if (this.length > value)
      do delete this.arr[--this.length];
      while (this.length > value);
    else
      do this.arr[this.length++] = 0;
      while (this.length < value);
  }
  public ToArrary(): number[] {
    let buf = new Array(this.length);
    for (let i = 0; i < this.length; i++) buf[i] = this.arr[i];
    return buf;
  }
}